package cn.xdwanj.ask.config;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

/**
 * @author xdwanj
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DatabaseDocConfig {
    private final DataSource dataSource;

    @PostConstruct
    public void docInit() {
        new DocumentationExecute(getScrewConfig())
            .execute();

        log.info("数据库文档生成完毕");
    }

    private EngineConfig getEngineConfig() {
        return EngineConfig.builder()
            // .fileOutputDir("/home/xdwanj/TEMP/doc")
            .openOutputDir(true)
            .fileType(EngineFileType.WORD)
            .produceType(EngineTemplateType.freemarker)
            .fileName("databaseDoc")
            .build();
    }

    private Configuration getScrewConfig() {
        return Configuration.builder()
            .version("v1.0.0")
            .description("ask数据库文档")
            .dataSource(dataSource)
            .engineConfig(getEngineConfig())
            .build();
    }
}
